package control;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BookingDTO;
import model.UserDTO;
import business.BookingManager;
import business.UserManager;
import business.vo.BookingVO;

/**
 * Servlet implementation class ApproveRejectServlet
 */
@WebServlet("/ApproveRejectServlet")
public class ApproveRejectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ApproveRejectServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) {
		BookingManager bm = new BookingManager();
		UserManager um= new UserManager();   
		simpleEmail mail = new simpleEmail();  
		
		BookingDTO book = null;
		book = bm.getBooking(Integer.parseInt(request.getParameter("bkid")));
        book.setComment(request.getParameter("comment"));
        
		if (request.getParameter("aa").equals("rejected")
				|| request.getParameter("aa").equals("approved")) {
			book.setStatus(request.getParameter("aa"));
		}
		bm.approveReject(book);
		UserSession userSession = (UserSession) request.getSession()
				.getAttribute("USERSESSION");
		ArrayList<BookingVO> data = bm.findBookingbyStatus(userSession.getUser().getUserID());		
		mail.setFrom(userSession.getUser().getEmail());
		request.setAttribute("bookinglist", data);
		RequestDispatcher rd = request.getRequestDispatcher("/Manager.jsp");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	       
	    UserDTO user= um.findUser(Integer.parseInt(request.getParameter("userid")));

	     mail.setM_to(user.getEmail());

	 if (request.getParameter("aa").equals("rejected")) 
	 {
		 mail.setM_text("Your booking request has been rejected. For details please login to http://localhost:8080/FacilityBookingSystem/login.jsp");}
	 else if (request.getParameter("aa").equals("approved")) 
	 {
		 mail.setM_text("Your booking request has been approved. For details please login to http://localhost:8080/FacilityBookingSystem/login.jsp");}
 
	  mail.sendMail();
	 
	 
	}

}
